package com.evl.ticketPrice.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.evl.common.controller.BaseController;
import com.evl.common.persistence.dao.StationMapper;
import com.evl.common.persistence.dao.TicketPriceMapper;
import com.evl.common.persistence.dao.VehicleMapper;
import com.evl.common.persistence.model.Station;
import com.evl.common.persistence.model.TicketPrice;
import com.evl.common.persistence.model.Vehicle;
import com.evl.core.shiro.ShiroKit;
import com.evl.core.shiro.ShiroUser;
import com.evl.core.vo.JsonReturnHelper;

/**
 * 票价管理控制器
 *
 * @author 
 * @Date 2017-07-13 15:03:06
 */
@Controller
@RequestMapping("/ticketPrice")
public class TicketPriceController extends BaseController {

    private String PREFIX = "/ticketPrice/";
    
    @Resource
    VehicleMapper vehicleMapper;
    
    @Resource
    StationMapper stationMapper;
    
    @Resource
    TicketPriceMapper ticketPriceMapper;

    /**
     * 跳转到票价管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "ticketPrice.html";
    }

    /**
     * 跳转到添加票价管理
     */
    @RequestMapping("/ticketPrice_add/{vehicleId}")
    public String ticketPriceAdd(@PathVariable Integer vehicleId, Model model) {
    	Vehicle vehicle = vehicleMapper.selectById(vehicleId);
    	model.addAttribute("vehicle", vehicle);
        return PREFIX + "ticketPrice_add.html";
    }

    /**
     * 跳转到修改票价管理
     */
    @RequestMapping("/ticketPrice_update/{vehicleId}/{ticketPriceId}")
    public String ticketPriceUpdate(@PathVariable Integer vehicleId,@PathVariable Integer ticketPriceId, Model model) {
    	Vehicle vehicle = vehicleMapper.selectById(vehicleId);
    	model.addAttribute("vehicle", vehicle);
    	
    	TicketPrice ticketPrice = ticketPriceMapper.selectTicketPriceById(ticketPriceId);
    	model.addAttribute("ticketPrice", ticketPrice);
        return PREFIX + "ticketPrice_edit.html";
    }

    /**
     * 获取票价管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return null;
    }

    /**
     * 新增票价管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TicketPrice ticketPrice) {
    	//设置车次的第一个站票价为全程票价
    	Station station = stationMapper.selectByVehicleId(ticketPrice.getVehicleId());
    	if(station ==null){
    		return JsonReturnHelper.getFailWithError("4001","无法查询车次对应的车站信息");
    	}
    	ticketPrice.setStationId(station.getId());
    	ticketPrice.setVehicleId(null);
    	
    	ShiroUser shiroUser = ShiroKit.getUser();
    	ticketPrice.setCreateId(shiroUser.getId());
    	ticketPrice.setCreateTime(new Date());
    	
        return  this.ticketPriceMapper.insert(ticketPrice);
    }

    /**
     * 删除票价管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer ticketPriceId) {
        return this.ticketPriceMapper.deleteById(ticketPriceId);
    }


    /**
     * 修改票价管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TicketPrice ticketPrice) {
    	//设置车次的第一个站票价为全程票价
    	ticketPrice.setVehicleId(null);
    	
    	ShiroUser shiroUser = ShiroKit.getUser();
    	ticketPrice.setUpdateId(shiroUser.getId());
    	
        return  this.ticketPriceMapper.updateById(ticketPrice);
    }

    /**
     * 票价管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
    	return super.SUCCESS_TIP;
    }
}
